<script setup>
import { ref, onMounted, watch, computed } from 'vue'
import { useUserStore } from '@/stores'
import { getCommentAPI, createCommentAPI } from '@/api/comment.js'
import { getFeedbackAPI, addOrDelFeedbackAPI } from '@/api/leaveword.js'
import { avatarColor } from '@/assets/data.js'
import { message } from '@/components/ZyMessage/index.js'
import { formatDateTime } from '@/utils/common.js'

const userStore = useUserStore()
const props = defineProps(['card'])
const emit = defineEmits(['refreshComment'])

// 用户是否登录
const isLogin = computed(() => Boolean(userStore.user.id))

// 获取当前卡片是否已举报
const isReport = ref(false)
const getFeedback = async () => {
  // 未登录
  if (!isLogin.value) {
    isReport.value = false
    return
  }
  const result = await getFeedbackAPI({ leaveword_id: props.card.id, type: 2 })
  isReport.value = result
}
//  举报/撤销举报
const onReport = async () => {
  if (!isLogin.value) return message('请先登录!', 'warning')
  await addOrDelFeedbackAPI({ leaveword_id: props.card.id, type: 2 })
  isReport.value = !isReport.value
}

// 获取评论
const loading = ref(true)
const commentList = ref([])
const getComment = async () => {
  loading.value = true
  try {
    commentList.value = await getCommentAPI(props.card.id)
  } finally {
    loading.value = false
  }
}

// 添加评论
const loadingBtn = ref(false)
const comment = ref('')
const onSubmit = async () => {
  if (!isLogin.value) return message('请先登录!', 'warning')
  loadingBtn.value = true
  try {
    await createCommentAPI({
      leaveword_id: props.card.id,
      content: comment.value
    })
    message('评论成功!')
    getComment()
    // 刷新评论数量
    emit('refreshComment', props.card.id)
    comment.value = ''
  } finally {
    loadingBtn.value = false
  }
}

// 留言卡片切换
watch(
  () => props.card,
  () => {
    getComment()
    getFeedback()
    comment.value = ''
  }
)

onMounted(() => {
  getComment()
  getFeedback()
})
</script>

<template>
  <div class="leaveword-detail">
    <!-- 举报 -->
    <div class="top">
      <p class="report" v-if="isReport" @click="onReport">撤销举报</p>
      <p class="report" v-else @click="onReport">举报</p>
    </div>
    <!-- 留言内容 -->
    <zy-card class="card" :card="card" :isEllipsis="false" />
    <!-- 发布评论 -->
    <div class="form">
      <textarea class="message" v-model="comment" placeholder="请输入..."></textarea>
      <div class="sign">
        <div class="name">
          {{ userStore.user?.nickname || userStore.user?.username || '评论人' }}
        </div>
        <zy-button class="submit" :loading="loadingBtn" @click="onSubmit">评论</zy-button>
      </div>
    </div>
    <!-- 评论列表 -->
    <div class="comment" v-zy-loading="loading">
      <div class="title">评论</div>
      <div class="item" v-for="item in commentList" :key="item.id">
        <!-- 判断头像是否有效 -->
        <img class="avatar" :src="item.user.avatar" v-if="item.user.avatar?.startsWith('http')" />
        <div
          class="avatar"
          v-else
          :style="{ backgroundImage: avatarColor[item.user.avatar] }"
        ></div>
        <!-- 右侧 -->
        <div class="right">
          <div class="top">
            <div class="name">{{ item.user.nickname || item.user.username }}</div>
            <div class="time">{{ formatDateTime(item.created_time) }}</div>
          </div>
          <div class="text">{{ item.content }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.leaveword-detail {
  padding: 0 20px;
}
.top {
  display: flex;
  align-items: center;
  .report {
    font-size: 14px;
    color: var(--warning-color);
    cursor: pointer;
  }
}
.card {
  width: 100%;
  margin: 10px 0;
}
.form {
  .message {
    background: none;
    min-height: 56px;
    border: 1px solid rgba(148, 148, 148, 1);
    resize: none;
    padding: 8px;
    width: 100%;
    color: var(--primary-text);
    &::placeholder {
      color: var(--gray-text-2);
    }
  }
  .sign {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .name {
      width: 200px;
      padding: 6px;
      color: var(--gray-text-2);
      border: 1px solid rgba(148, 148, 148, 1);
      cursor: not-allowed;
    }
    .submit {
      width: 82px;
      height: 36px;
      border-radius: 18px;
      color: var(--drawer-btn-text);
      background-color: var(--primary-text);
      opacity: 0.6;
      cursor: pointer;
    }
  }
}
.comment {
  min-height: 60px;
  .title {
    font-weight: 600px;
    margin-top: 30px;
    padding-bottom: 20px;
    color: var(--primary-text);
  }
  .item {
    display: flex;
    margin-bottom: 20px;
    .avatar {
      flex-shrink: 0;
      width: 28px;
      height: 28px;
      border-radius: 50%;
      margin-right: 8px;
      overflow: hidden;
    }
    .right {
      .top {
        display: flex;
        .name {
          font-weight: 600;
          color: var(--primary-text);
        }
        .time {
          font-size: 12px;
          margin-left: 4px;
          color: var(--gray-text-2);
        }
      }
      .text {
        margin-top: 4px;
        color: var(--gray-text-1);
      }
    }
  }
}
</style>
